<template>
  <div class="main">
    <h1 style="text-align: center;">学生成绩表</h1>
    <el-button type="primary" style="text-align: center;" @click="handleAdd()">新增</el-button>
    <el-button type="primary" style="text-align: center;" @click="handleCheck()">查询</el-button>
    <br><br>
    <el-table :data="tableData" border style="width: 50%">
      <el-table-column prop="id" label="学号" width="180" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="score" label="分数" width="180" />
      <el-table-column prop="desc" label="评语" />
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑模态框 开始 -->
    <el-dialog v-model="dialogFormVisible" title="编辑" width="500">
      <el-form :model="form">
        <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="form.id" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input-number :min="0" :max="100" v-model="form.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="form.desc" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="editUser">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 编辑模态框 结束 -->
     <!-- 新增模态框 开始 -->
      <el-dialog v-model="addDialogFormVisible" title="新增" width="500">
        <el-form :model="addForm">
          <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="form.id" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input-number :min="0" :max="100" v-model="form.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="form.desc" type="textarea" autocomplete="off" />
        </el-form-item>
        </el-form>
        <template #footer>
        <div class="dialog-footer">
          <el-button @click="addDialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addUser">
            确认
          </el-button>
        </div>
      </template>
      </el-dialog>
      <!-- 新增模态框 结束 -->
       <!-- 查询模态框 开始 -->
        <el-dialog v-model="checkDialogFormVisible" title="查询" width="500">
          <el-form :model="checkForm">
            <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="form.id" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        </el-form>
        <template #footer>
        <div class="dialog-footer">
          <el-button @click="checkDialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="checkUser">
            查询
          </el-button>
        </div>
      </template>
        </el-dialog>
        <!-- 查询模态框 结束 -->
  </div>
</template>

<script setup>
import { ref } from 'vue'
const tableData = ref([
  {
    id: '001',
    name: '张三',
    score: 90,
    desc: "很优秀"
  },
  {
    id: '002',
    name: '李四',
    score: 80,
    desc: "优秀"
  },
  {
    id: '003',
    name: '王五',
    score: 70,
    desc: "良好"
  },
  {
    id: '004',
    name: '赵六',
    score: 98,
    desc: "666"
  }
])
const formLabelWidth = '140px'
const form = ref({});
// 编辑的模态框
const dialogFormVisible = ref(false);
function handleDelete (id) {
  // console.log('🤡 CC - handleDelete - id:', id);
  // 发送请求 delStudentById(id);
  tableData.value.forEach((e, index) => {
    if (e.id == id) {
      // console.log('🤡 CC - tableData.value.forEach - index:', index);
      // console.log('🤡 CC - handleDelete - e:', e);
      // 根据下标删除数组元素
      tableData.value.splice(index, 1);
    }
  })
}
// 打开编辑表单
function handleEdit (row) {
  // console.log('🤡 CC - handleEdit - row:', row);
  form.value = row;
  dialogFormVisible.value = true
}
// 提交编辑表单
function editUser () {
  // 将数据发送到后台
  // editStudentById(form.value);
  dialogFormVisible.value = false
}
// 新增的模态框
const addForm = ref({});
const addDialogFormVisible = ref(false);
// 打开新增表单
function handleAdd () {
  // addForm.value=row;
  addDialogFormVisible.value = true;
}
// 提交新增表单
function addUser () {
  tableData.value.push({
    id: form.value.id,
    name: form.value.name,
    score: form.value.score,
    desc: form.value.desc
  })
  addDialogFormVisible.value = false
}
// 查询的模态框
const checkForm = ref({});
const checkDialogFormVisible = ref(false);
// 打开查询表单
function handleCheck () {
  checkDialogFormVisible.value = true;
}
// 提交查询表单
function checkUser () {
  // 先根据用户输入的学号和姓名进行筛选
  const filteredData = tableData.value.filter(item => {
    return item.id === form.value.id && item.name === form.value.name
  });

  // 将筛选后的结果更新到tableData中，以便表格重新渲染显示筛选后的内容
  tableData.value = filteredData;

  // 这里可以根据实际需求进行更多操作，比如判断是否找到了符合条件的学生记录
  if (filteredData.length > 0) {
    console.log('找到符合条件的学生记录：', filteredData);
  } else {
    console.log('未找到符合条件的学生记录');
  }

  // 关闭查询模态框
  checkDialogFormVisible.value = false;
}
</script>

<style scoped>
.main {
  height: 100%;
  text-align: center;
  /* border: 1px solid red; */
  padding-left: 10%;
  padding-top: 10%;
}
</style>